package com.xiaoma.verbal.service;

import com.xiaoma.verbal.dao.QuCourseDao;
import com.xiaoma.verbal.domain.ForcastQuestionInfo;
import com.xiaoma.verbal.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * Created by Owner on 2015/5/20.
 */
@Service
public class QuCourseService {
    @Autowired
    private QuCourseDao quCourseDao;

    public int importData(int version, InputStream fis){
        List<Map<String, Object>> list = null;
        try {
            // excel数据
            if (version == 2003) {
                list = ExcelUtil.parseCommonInfoExcel_2003(fis);
            } else {
                list = ExcelUtil.parseCommonInfoExcel_2007(fis);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 2;
        }
        ForcastQuestionInfo qc = null;
        if(quCourseDao.findAll().size()>0){
            quCourseDao.deleteAll();
        }
        for( Map<String,Object> data : list){
            qc = new ForcastQuestionInfo();
            qc.setAudioSeqNum((int) Double.parseDouble((String) data.get("audio_seq_num")));
            qc.setAudioCode((String) data.get("audio_code"));
            qc.setAudioContent((String) data.get("audio_content"));
            qc.setQuestionNum((String) data.get("question_num"));
            qc.setAudioScode((String) data.get("audio_scode"));
            quCourseDao.insert(qc);
        }
        return 1;

    }
}
